// 日期格式转换
export const DATEFORMAT = (t: string | number | Date,fmt: string) => {
    const d = new Date(t);
    const o = {
        "M+":d.getMonth()+1,//月份
        "d+":d.getDate(),//日
        "H+":d.getHours(),//小时
        "m+":d.getMinutes(),//分
        "s+":d.getSeconds(),//秒
        "q+":Math.floor((d.getMonth()+3)/3),//
        "S":d.getMilliseconds()//毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1,(d.getFullYear()+"").substr(4-RegExp.$1.length));
    for(const k in o)
        if(new RegExp("("+k+")").test(fmt))
        fmt=fmt.replace(RegExp.$1,(RegExp.$1.length==1)? (o[k]) : (("00"+o[k]).substr((""+o[k]).length)));
    return fmt;
};

// 深拷贝
export const deepCopy = (obj: any) :any => {
    if (obj instanceof Object) {
      const newObj: any = {}
      if (Array.isArray(obj)) {
        const arr: any[] = []
        obj.forEach(item => {
          arr.push(deepCopy(item))
        })
        return arr
      } else {
        for (const key in obj) {
          const value = obj[key]
          if (typeof value === 'function') {
            newObj[key] = value.bind(newObj)
          } else if (typeof value === 'object') {
            if (Array.isArray(value)) {
              newObj[key] = []
              value.forEach(item => {
                newObj[key].push(deepCopy(item))
              })
            } else {
              newObj[key] = deepCopy(value)
            }
          } else {
            newObj[key] = value
          }
        }
      }
      return newObj
    } else {
      return obj
    }
}
  